﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetXianHandler : IRequestHandler<GetXianCommand, ApiResult<List<XianDTO>>>
    {
        private readonly IBaseRepository<XianModel> xianRepo;
        private readonly IMapper mapper;

        public GetXianHandler(IBaseRepository<XianModel> xianRepo, IMapper mapper)
        {
            this.xianRepo = xianRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<XianDTO>>> Handle(GetXianCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<XianDTO>> res = new ApiResult<List<XianDTO>>();
            var xian = xianRepo.GetAll().Where(x => x.ShiId == request.ShiId);
            res.Data = mapper.Map<List<XianDTO>>(xian);
            res.Code = ApiEnum.Success;
            res.Msg = "显示县级信息成功";
            return Task.FromResult(res);
        }
    }
}
